import sqlite3
import json

from utils.config_manager import config


def get_json_value(json_path, user_id=1, db_path="robot_zl.db"):
    db_path =  config.get_db_path()

    """从 datajson 中提取指定路径的值"""
    if not json_path.startswith("$."):
        json_path = "$." + json_path

    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            SELECT json_extract(datajson, ?) as value 
            FROM gt 
            WHERE userId = ?
        ''', (json_path, user_id))

        result = cursor.fetchone()
        return result[0] if result else None


def get_data(user_id=1, db_path="robot_zl.db"):
    """从 datajson 中提取指定路径的值"""
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            SELECT datajson 
            FROM gt 
            WHERE userId = ?
        ''', (user_id,))

        result = cursor.fetchone()
        return json.loads(result[0]) if result else None
